package com.sgb.kjwl.view.ui.activity.manage

import android.content.Context
import android.content.Intent
import android.text.Html
import android.text.TextUtils
import android.view.View
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityWebsiteOrganizationBinding
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent
import com.sgb.kjwl.di.manage.EnterpriseManageModule
import com.sgb.kjwl.model.entity.manage.WebsiteInfoEntity
import com.sgb.kjwl.utils.ReplaceUtils
import com.sgb.kjwl.view.ui.adapter.manage.TagAdapter
import com.sgb.kjwl.view.widget.manage.MImgGetter
import com.sgb.kjwl.view.widget.manage.WebsiteUtils
import com.sgb.kjwl.viewmodel.manage.IntroductionForWebsiteModel
import com.swgk.core.base.di.AppComponent
import com.swgk.core.util.ClickUtils
import com.swgk.core.util.SharedPreferenceUtil
import java.util.*
import javax.inject.Inject

/**
 * 组织--信息简介
 */
class WebsiteOrganizationActivity : AppActivity() {
    private lateinit var mBinding : ActivityWebsiteOrganizationBinding

    @Inject
    lateinit var viewModel: IntroductionForWebsiteModel

    private val noData = "--"
    private val noDataY = "--"

    private var mEntity: WebsiteInfoEntity ?= null

    private lateinit var mAdapter : TagAdapter

    companion object {
        const val REQUEST_CODE = 10200
        fun launch(context: Context) {
            val intent = Intent(context, WebsiteOrganizationActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        super.setupActivityComponent(appComponent)
        DaggerEnterpriseManageComponent.builder().appComponent(appComponent)
            .enterpriseManageModule(EnterpriseManageModule()).build().inject(this)
    }


    override fun initView() {
        super.initView()
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_website_organization)
        mBinding.include.titleView.text = "政府/事业单位信息"
        mBinding.tvUpdate.setOnClickListener(this)
        mBinding.include.ivBack.setOnClickListener(this)
        mBinding.rlVideoView.setOnClickListener(this)
        initRefreshView()
        viewModel.websiteInfoEntityMutableLiveData.observe(this, {
            if (it == null) return@observe
            mEntity = it
            mBinding.viewModel = it
            showWebsiteInfo(it)
        })
    }

    override fun onClick(v: View) {
        super.onClick(v)
        when (v.id) {
            R.id.iv_back -> finish()
            R.id.tv_update ->
                if (!ClickUtils.isDoubleClick()) {
                    WebsiteUpDateOrganizationActivity.launch(this, REQUEST_CODE)
                }
            R.id.rl_video_view -> {
                WebsiteUtils.getInstance().openVideo(this, mBinding.viewModel?.video, mBinding.viewModel?.videoUrl, mBinding.viewModel?.coverUrl)
            }
        }
    }



    private fun initRefreshView() {
        mBinding.swipeRefresh.setEnableLoadMore(false)
        // 刷新
        mBinding.swipeRefresh.setOnRefreshListener(OnRefreshListener {
            init()
            mBinding.swipeRefresh.finishRefresh(500)
        })
        mAdapter = TagAdapter(this, R.layout.item_tag_website_info, ArrayList())
        mBinding.recyclerViewTag.setAdapter(mAdapter)
    }

    override fun init() {
        super.init()
        //网店编号
        val companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "")
        viewModel.getWebsiteInfo(companyNo, 3) //组织信息

    }


    fun showWebsiteInfo(entity: WebsiteInfoEntity) {
        settitleView(entity)
        if (TextUtils.isEmpty(entity.organizers)) {
            mBinding.tvOrganizer.text = noData
        } else {
            mBinding.tvOrganizer.text = entity.organizers
        }
//        if (TextUtils.isEmpty(entity.institutionalTypeName)) {
//            mBinding.tvIndustryCategory.text = noData
//        } else {
//            mBinding.tvIndustryCategory.text = entity.institutionalTypeName
//        }
        if (TextUtils.isEmpty(entity.fixedPhone)) {
            mBinding.tvFixedNumber.text = noData
        } else {
            mBinding.tvFixedNumber.text = entity.fixedPhone
        }
        if (TextUtils.isEmpty(entity.contactPerson)) {
            mBinding.tvConcat.text = noData
        } else {
            mBinding.tvConcat.text = entity.contactPerson
        }
        if (TextUtils.isEmpty(entity.contactPhone)) {
            mBinding.tvPhone.text = noData
        } else {
            mBinding.tvPhone.text = entity.contactPhone
        }

        if (TextUtils.isEmpty(entity.address)) {
            mBinding.tvAddress.text = noData
        } else {
            mBinding.tvAddress.text = entity.address
        }
        if (TextUtils.isEmpty(entity.registrationDepartment)) {
            mBinding.tvDepartment.text = noData
        } else {
            mBinding.tvDepartment.text = entity.registrationDepartment
        }
        if (TextUtils.isEmpty(entity.businessScope)) {
            mBinding.tvBusinessScope.text = noData
        } else {
            mBinding.tvBusinessScope.text = entity.businessScope
        }
        if (TextUtils.isEmpty(entity.purpose)) {
            mBinding.tvPurpose.text = noData
        } else {
            mBinding.tvPurpose.text = entity.purpose
        }
        if (TextUtils.isEmpty(entity.introduction)) {
            mBinding.tvIntroduction.text = noData
            mBinding.linIntroduction.visibility = View.GONE
        } else {
            mBinding.linIntroduction.visibility = View.VISIBLE
            mBinding.tvIntroduction.text = Html.fromHtml(
                ReplaceUtils.getHtml(entity.introduction), MImgGetter(
                    activity, mBinding.tvIntroduction
                ), null
            )
        }
    }


    private fun settitleView(entity: WebsiteInfoEntity) {
        if (!TextUtils.isEmpty(entity.logo)) {
            val options = RequestOptions()
                .placeholder(R.drawable.icon_organization_default) //图片加载出来前，显示的图片
                .error(R.drawable.icon_organization_default) //图片加载失败后，显示的图片
                .transform(CircleCrop())
            Glide.with(this).load(entity.logo).apply(options)
                .into(mBinding.ivShopAvatar)
        }
        entity.serviceTypeName = entity.institutionalTypeName
        if (!TextUtils.isEmpty(entity.serviceTypeName)) {
            val serviceNameList: MutableList<String> = ArrayList()
            serviceNameList.addAll(entity.getServiceTypeList(5))
            mAdapter.data = serviceNameList
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK){
            if (data != null && data.getSerializableExtra(WebsiteUpDateOrganizationActivity.KEY_DATA) is WebsiteInfoEntity){
                val entity = data.getSerializableExtra(WebsiteUpDateOrganizationActivity.KEY_DATA) as WebsiteInfoEntity
                mBinding.viewModel = entity
                showWebsiteInfo(entity)
            }
        }

    }

}